'use strict';

/**
 * @ngdoc overview
 * @name appApp
 * @description
 * # appApp
 *
 * Main module of the application.
 */
angular
  .module('appApp', [
    'ngAnimate',
    'ngAria',
    'ngCookies',
    'ngResource',
    'ngRoute',
    'ngSanitize',
    'ngTouch',
    'ui.bootstrap'
  ])
  .run(function($rootScope, $location, $route, onlineFac) {
    $rootScope.$on('$routeChangeSuccess', function(angularEvent,current,previous){
      var title = current && current.$$route && current.$$route.title;
      if(!title){
        document.title = '瑞怡乳腺';
      }
      document.title = title;
      var $body = $('body');
      // hack在微信等webview中无法修改document.title的情况
      var $iframe = $('<iframe src="" style="display:none"></iframe>').on('load', function() {
        setTimeout(function() {
          $iframe.off('load').remove()
        }, 0)
      }).appendTo($body)

      // 获取当前页面路径，用于导航nav高亮
      $rootScope.currentPath = $rootScope.getCurrentPath($route.current.originalPath);
      $rootScope.currentPatientPath = $rootScope.getPatientPath($route.current.originalPath);

      // 滚动回顶部
      $rootScope.gotoTop();

    });
    
    $rootScope.loopTo = function(url){
      $location.url(url);
    };

    $rootScope.loopToNew = function(url){
      window.open(url,'_blank');
    };

    $rootScope.getCurrentPath = function(url){
      var path = url.substr(1).split('/');
      return(path[0]);
    }

    $rootScope.getPatientPath = function(){
      var path = $route.current.originalPath.substr(1).split('/');
      return(path[1]);
    }

    $rootScope.gotoTop = function() {
      $('body,html').scrollTop(0);
    };

    $rootScope.patientmenu = [{
      title:'专家预约',
      name:'appointment',
      icon:'icon-zjyy'
    },{
      title:'在线咨询',
      name:'consult',
      icon:'icon-zxzx'
    },{
      title:'报告解读',
      name:'report',
      icon:'icon-bgjd'
    },{
      title:'MDT会诊',
      name:'mdt',
      icon:'icon-meeting'
    },{
      title:'术后护理',
      name:'nursing',
      icon:'icon-shhl'
    },{
      title:'用药指南',
      name:'medicine',
      icon:'icon-yyzn'
    },{
      title:'临床招募',
      name:'clinical',
      icon:'icon-lczm'
    },{
      title:'慈善救助',
      name:'charity',
      icon:'icon-csjz'
    }];

    $rootScope.profilemenu = [{
      title:'申请管理',
      icon:'icon-profile-list',
      children: [{
        title: '专家预约申请',
        url: 'profile-appointment'
      },{
        title: '在线咨询申请',
        url: 'profile-consult'
      },{
        title: '报告解读申请',
        url: 'profile-report'
      },{
        title: 'MDT会诊申请',
        url: 'profile-mdt'
      },{
        title: '临床招募申请',
        url: 'profile-clinical'
      },{
        title: '慈善救助申请',
        url: 'profile-charity'
      }]
    },{
      title:'健康档案',
      icon:'icon-health',
      children: [{
        title: '家庭联系人',
        url: 'profile-contact'
      }]
    },{
      title:'我的收藏',
      icon:'icon-collection',
      children: [{
        title: '我的文章',
        url: 'profile-article'
      }]
    },{
      title:'更多设置',
      icon:'icon-setting',
      children: [{
        title: '账号设置',
        url: 'profile'
      },{
        title: '意见反馈',
        url: 'profile-feedback'
      }]
    }];
  })
  .config(function ($routeProvider,$locationProvider) {
    $locationProvider.hashPrefix('');
    $routeProvider
      .when('/index', {
        title: '瑞怡乳腺',
        templateUrl: 'views/index.html',
        controller: 'IndexCtrl',
        controllerAs: 'index'
      })
      .when('/about', {
        title: '瑞怡乳腺',
        templateUrl: 'views/about.html',
        controller: 'AboutCtrl',
        controllerAs: 'about'
      })
      .when('/resource/center', {
        title: '瑞怡乳腺',
        templateUrl: 'views/resource/center.html',
        controller: 'ResourceCenterCtrl',
        controllerAs: 'Resource/center'
      })
      .when('/resource/article-detail/:articleId', {
        title: '瑞怡乳腺',
        templateUrl: 'views/resource/article-detail.html',
        controller: 'ResourceArticleDetailCtrl',
        controllerAs: 'Resource/articleDetail'
      })
      .when('/resource/article-list/:listId', {
        title: '瑞怡乳腺',
        templateUrl: 'views/resource/article-list.html',
        controller: 'ResourceArticleListCtrl',
        controllerAs: 'resource/articleList'
      })
      .when('/profession/space', {
        title: '瑞怡乳腺',
        templateUrl: 'views/profession/space.html',
        controller: 'ProfessionSpaceCtrl',
        controllerAs: 'profession/space'
      })
      .when('/profession/profession-detail/:articleId', {
        title: '瑞怡乳腺',
        templateUrl: 'views/profession/profession_detail.html',
        controller: 'ProfessionProfessionDetailCtrl',
        controllerAs: 'profession/professionDetail'
      })
      .when('/patient/appointment', {
        title: '瑞怡乳腺',
        templateUrl: 'views/patient/appointment.html',
        controller: 'PatientAppointmentCtrl',
        controllerAs: 'patient/appointment'
      })
      .when('/public/patient-leftmenu', {
        templateUrl: 'views/public/patient-leftmenu.html',
        controller: 'PublicPatientLeftmenuCtrl',
        controllerAs: 'public/patientLeftmenu'
      })
      .when('/patient/consult', {
        title: '瑞怡乳腺',
        templateUrl: 'views/patient/consult.html',
        controller: 'PatientConsultCtrl',
        controllerAs: 'patient/consult'
      })
      .when('/profile/profile', {
        title: '瑞怡乳腺',
        templateUrl: 'views/profile/profile.html',
        controller: 'ProfileProfileCtrl',
        controllerAs: 'profile/profile'
      })
      .when('/profile/profile-leftmenu', {
        templateUrl: 'views/profile/profile-leftmenu.html',
        controller: 'ProfileProfileLeftmenuCtrl',
        controllerAs: 'profile/profileLeftmenu'
      })
      .when('/profile/faq/:faqId', {
        title: '瑞怡乳腺',
        templateUrl: 'views/profile/faq.html',
        controller: 'ProfileFaqCtrl',
        controllerAs: 'profile/faq'
      })
      .when('/profile/faq-leftmenu', {
        templateUrl: 'views/profile/faq-leftmenu.html',
        controller: 'ProfileFaqLeftmenuCtrl',
        controllerAs: 'profile/faqLeftmenu'
      })
      .otherwise({
        redirectTo: '/index'
      });
  });
